<?php

namespace app\home\controller;

use app\common\libs\Area;
use app\common\model\virtual\VirtualScanTempLogModel;
use app\common\validate\shop\ShopSalesmanValid;
use app\common\validate\shop\ShopValid;
use app\common\validate\system\SystemApiValid;
use app\common\validate\virtual\VirtualScanTempLogValid;
use app\home\loader\Services;
use app\merchants\loader\Services as merchantsServices;
use think\Exception;

class System extends ApiBase
{
    /**
     * 商户入口
     * @return string
     */
    public function entry(): string
    {
        try {
            $mer_id = input('get.key');
            if (empty($mer_id)) {
                return '商户入口地址不正确，请联系经销商.';
            } else {
                $mer_id = base64_de($mer_id);
                Services::System()->entry($mer_id);
            }
        } catch (Exception $e) {
            return '微信认证异常，请重试';
        }
    }

    /**
     * 微信登录
     */
    public function wx_login()
    {
        try {
            $params = $this->request->post();
            if (empty($params['code'])) {
                api_exception(400, 'code不能为空');
            }
            if (empty($params['mkey'])) {
                api_exception(400, '商户编码不能为空');
            }
            $result = Services::System()->wxLogin($params, (int)$this->request_app);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     *  导购注册
     */
    public function wx_register()
    {
        try {
            $params = $this->request->post();
            validate(ShopSalesmanValid::class)->scene('wx_register')->check($params);
            if (empty($params['lng']) || empty($params['lat'])){
                throw new Exception('请先开启定位权限',API_ERROR);
            }
            $params['address'] = Area::getInstance($params['lng'], $params['lat'])->getLocationLat();
            $result = Services::System()->wxRegisterSalesMan($params);
            api_result($result['result'], $result['msg'], $result['data']);
        } catch (\think\Exception $e) {
            api_result($e);
        }
    }


    /**
     * 上传
     */
    public function upload()
    {
        try {
            $info = merchantsServices::System()->uploadFile();

            api_result(API_SUCCESS, '请求成功', $info);
        } catch (\think\Exception $e) {
            api_result($e);
        }
    }

    /**
     * 微信JSSDK使用配置信息
     */
    public function get_config()
    {
        try {
            $params = $this->request->post();
            if (empty($params['url'])) {
                api_exception(400, 'url不能为空');
            }
            $result = Services::System()->getConfig($params);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 门店列表
     */
    public function shop_list()
    {
        try {
            $params = $this->request->get();
            validate(ShopValid::class)->scene('shop_list')->check($params);
            if (!empty($params['keyword'])){
                $where[] = ['title' ,'like','%'.$params['keyword'].'%'];
            }
            if (!empty($params['mer_id'])){
                $where[] = ['mer_id' ,'=',$params['mer_id']];
            }
            $params['where'] = $where;
            $result = Services::System()->getShopList($params);
            api_result($result);
        } catch (Exception $e) {
            api_result($e);
        }
    }



    /**
     * 获取位置
     */
    public function get_location()
    {
        $params = input('get.');
        validate(ShopSalesmanValid::class)->scene('get_location')->check($params);
        $address = Area::getInstance($params['lng'], $params['lat'])->getLocationLat();
        api_result(API_SUCCESS, '请求成功', ['address' => $address]);
    }


    /**
     * 用户信息
     * @return void
     * @throws \yiqiniu\extend\exception\ApiException
     */
    public function user_info()
    {
        try {
            $result = Services::System()->userInfo(
                $this->merId,
                $this->shopUser['user_id']);
            api_result(API_SUCCESS, '请求成功', $result);
        } catch (Exception $e) {
            api_result(API_ERROR, $e->getMessage());
        }
    }
}
